home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / GCC / V2-4-5 / GPPLIBSR00 / cc / stdstrbufs < prev    next >
Text File  |  1993-12-07  |  4KB  |  123 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1992 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include "ioprivate.h"
  19.  
  20. extern "C"
  21.   {
  22.   #include <stdio.h>
  23.   }
  24.  
  25.  
  26. // This file defines the standard streambufs, corresponding to cin, cout, cerr.
  27. // We define two sets:
  28. //
  29. // __std_filebuf_0, __std_filebuf_1, __std_filebuf_2 are filebufs using
  30. // file descriptor 0/1/2.
  31. //
  32. // __stdin_stdiobuf, __stdout_stdiobuf, __stderr_stdiobuf are stdiostreams
  33. // pointing to stdin, stdout, stderr.
  34.  
  35.  
  36. // To avoid problems depending on constructor order (and for
  37. // efficiency) the standard streambufs (and streams) are
  38. // constructed statically using C-style '{ ... }' initializers.
  39. // Since you're not allowed to do this for structs that
  40. // have virtuals, we define fake streambuf and stream classes
  41. // that don't have any C++-isms, and initialize those.
  42. // To initialize the vtable field of the standard filebufs,
  43. // we use the expression 'vt_filebuf' which must evaluate to
  44. // (the address of) the virtual function table for the
  45. // filebuf class.
  46.  
  47. // added FO
  48. #define _G_NAMES_HAVE_UNDERSCORE 0
  49. #define _G_DOLLAR_IN_LABEL 0
  50.  
  51. #if _G_NAMES_HAVE_UNDERSCORE
  52. #define UNDERSCORE "_"
  53. #else
  54. #define UNDERSCORE ""
  55. #endif
  56.  
  57. // First define the filebuf-based objects.
  58.  
  59. #if !defined(vt_filebuf)
  60. #ifndef __GNUG__
  61. // This works for cfront.
  62. #define vt_filebuf __vtbl__7filebuf
  63. extern char vt_filebuf[1];
  64. #elif _G_DOLLAR_IN_LABEL
  65. extern char vt_filebuf[1] asm(UNDERSCORE "_vt$filebuf");
  66. #else
  67. extern char vt_filebuf[1] asm(UNDERSCORE "_vt.filebuf");
  68. #endif
  69. #endif /* !defined(vt_filebuf) */
  70.  
  71. struct _fake_filebuf {
  72.     struct __streambuf s;
  73.     char* vtable;
  74.     struct __file_fields f;
  75. };
  76.  
  77. #define FILEBUF_LITERAL(CHAIN, FLAGS) \
  78.        { _IO_MAGIC+_S_LINKED+_S_IS_FILEBUF+_S_IS_BACKUPBUF+FLAGS, \
  79.      0, 0, 0, 0, 0, 0, 0, 0, CHAIN, 0, 0, 0, 0, 0}
  80.  
  81. #define DEF_FILEBUF(NAME, FD, CHAIN, FLAGS) \
  82.   _fake_filebuf NAME[1] = {{FILEBUF_LITERAL(CHAIN, FLAGS), vt_filebuf, {FD}}};
  83.  
  84. DEF_FILEBUF(__std_filebuf_0, 0, 0, _S_NO_WRITES);
  85. DEF_FILEBUF(__std_filebuf_1, 1, (streambuf*)__std_filebuf_0, _S_NO_READS);
  86. DEF_FILEBUF(__std_filebuf_2, 2, (streambuf*)__std_filebuf_1,
  87.         _S_NO_READS+_S_UNBUFFERED);
  88.  
  89. // Nest define the stdiobuf-bases objects.
  90.  
  91. #if !defined(vt_stdiobuf)
  92. #ifndef __GNUG__
  93. // This works for cfront.
  94. #define vt_stdiobuf __vtbl__7stdiobuf
  95. extern char vt_stdiobuf[1];
  96. #elif _G_DOLLAR_IN_LABEL
  97. extern char vt_stdiobuf[1] asm(UNDERSCORE "_vt$stdiobuf");
  98. #else
  99. extern char vt_stdiobuf[1] asm(UNDERSCORE "_vt.stdiobuf");
  100. #endif
  101. #endif /* !defined(vt_stdiobuf) */
  102.  
  103. struct _fake_stdiobuf {
  104.     struct __streambuf s;
  105.     char* vtable;
  106.     struct __file_fields f;
  107.     FILE *_f;
  108. };
  109.  
  110. #define DEF_STDIOBUF(NAME, FILE, FD, CHAIN, FLAGS) \
  111.     _fake_stdiobuf NAME[1] = {{ \
  112.      FILEBUF_LITERAL(CHAIN, (FLAGS)|_S_UNBUFFERED),\
  113.      vt_stdiobuf, {FD}, FILE}};
  114.  
  115. DEF_STDIOBUF(__stdin_stdiobuf, stdin, 0, (streambuf*)__std_filebuf_2,
  116.          _S_NO_WRITES);
  117. DEF_STDIOBUF(__stdout_stdiobuf, stdout, 1, (streambuf*)__stdin_stdiobuf,
  118.          _S_NO_READS);
  119. DEF_STDIOBUF(__stderr_stdiobuf, stderr, 2, (streambuf*)__stdout_stdiobuf,
  120.          _S_NO_READS);
  121.  
  122. streambuf* streambuf::_list_all = (streambuf*)__stderr_stdiobuf;
  123.